﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Reflection;
/*
 * Project Home : http://code.google.com/p/klsudoku
 * Project Owner: ttylikl@gmail.com Email:ttylikl@qq.com
 * Project Blog : http://www.cnblogs.com/ttylikl
 * All Files of this project is free to use , but you should keep the information above when you copy&use it.
 * Infomation Modify Date: 2009.02.22
 * 
 * */

namespace SudokuPanel
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();

        }
        ConfigInfo ci = ConfigInfo.Instance();

        private void buttonOK_Click(object sender, EventArgs e)
        {
            for(int i=1;i<=lvlRadios.Length;++i)
            {
                if(lvlRadios[i-1].Checked)
                {
                    if (oldlvl == i)
                        break;
                    ci["lvl"] = i.ToString();
                    ci.saveConfigToXML();
                    break;
                }
            }
            if (radioFormatType91.Checked)
                ci["PuzzleStringFormat"] = "1";// pubfun.PuzzleStringFormat.FormatLine91;
            if (radioFormatType9x9.Checked)
                ci["PuzzleStringFormat"] = "2";// pubfun.PuzzleStringFormat.FormatMatrix9x9;
            if (radioFormatTypeTableAscii.Checked)
                ci["PuzzleStringFormat"] = "3";// pubfun.PuzzleStringFormat.FormatTableAscii;
            Close();
        }

        private System.Windows.Forms.RadioButton[] lvlRadios;
        private System.Windows.Forms.GroupBox groupLvl;
        private int oldlvl = 0;

        private void Config_Load(object sender, EventArgs e)
        {
            ConfigInfo ci = ConfigInfo.Instance();
            lvlRadios = new RadioButton[ConfigInfo.lvlList.Length];
            oldlvl = pubfun.str2int(ci["lvl", "1"]);
            if (oldlvl < 1 || oldlvl > ConfigInfo.lvlList.Length)
            {
                oldlvl = 1;
                ci["lvl"] = oldlvl.ToString();
            }

            for (int i = 0; i < ConfigInfo.lvlList.Length; ++i)
            {
                lvlRadios[i] = new RadioButton();
                lvlRadios[i].Checked = false;
                lvlRadios[i].AutoSize = true;
                lvlRadios[i].Location = new System.Drawing.Point(7, 14+i*16);
                lvlRadios[i].Name = "lvlRadio" + i;
                lvlRadios[i].Size = new System.Drawing.Size(75, 16);
                lvlRadios[i].TabIndex = i + 1;
                lvlRadios[i].TabStop = true;
                lvlRadios[i].Text = ConfigInfo.lvlList[i];
                lvlRadios[i].UseVisualStyleBackColor = true;
                this.groupLvl.Controls.Add(lvlRadios[i]);

            }
            // 
            // groupLvl
            // 
            this.groupLvl.Location = new System.Drawing.Point(12, 12);
            this.groupLvl.Name = "groupLvl";
            //this.groupLvl.Size = new System.Drawing.Size(140, 16 + lvlRadios.Length*16);
            this.groupLvl.Height =  16 + lvlRadios.Length * 16;
            this.groupLvl.TabIndex = 0;
            this.groupLvl.TabStop = false;
            this.groupLvl.Text = "游戏难度";
            
            lvlRadios[oldlvl-1].Checked = true;
            lvlRadios[oldlvl-1].Focus();

            //checkBoxShowCandidates.Left = groupLvl.Left;
            //checkBoxShowCandidates.Top = groupLvl.Bottom + 3;
            //checkBoxShowTagCandidates.Left = groupLvl.Left;
            //checkBoxShowTagCandidates.Top = checkBoxShowCandidates.Bottom + 3;
            checkBoxShowCandidates.Checked = (pubfun.str2int(ci["show_candidates", "1"]) == 1);
            checkBoxShowTagCandidates.Checked = (pubfun.str2int(ci["show_tag_candidates", "0"]) == 1);
            chkAutoPlayNext.Checked = (pubfun.str2int(ci["auto_play_next", "0"]) == 1);
            cbShowHotNumber.Checked = (pubfun.str2int(ci["show_hot_number", "1"]) == 1);
            
            chkUseKeyBoard.Checked = (pubfun.str2int(ci["use_keyboard", "0"]) == 1);
            update_checkHotSpot();

            chkShowScoreListOnOver.Checked = (pubfun.str2int(ci["game_finished_scorelist", "1"]) == 1);
            chkShowStatisticsOnOver.Checked = (pubfun.str2int(ci["game_finished_statistics", "1"]) == 1);

            groupBoxCopyFormat.Top=groupLvl.Top;
            groupBoxCopyFormat.Height = groupLvl.Height;
            switch(ci["PuzzleStringFormat","3"])
            {
                case "1": radioFormatType91.Checked = true;break;
                case "2": radioFormatType9x9.Checked = true; break;
                case "3": radioFormatTypeTableAscii.Checked = true; break;
            }

            //this.Size = new System.Drawing.Size(143 + buttonOK.Width + 30, groupLvl.Height + 100);
        }

        private void checkBoxShowCandidates_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb=(CheckBox)sender;
            ci["show_candidates"] = (cb.Checked) ? "1" : "0";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["show_tag_candidates"] = (cb.Checked) ? "1" : "0";
        }

        private void cbShowHotNumber_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["show_hot_number"] = (cb.Checked) ? "1" : "0";
        }

        private void chkAutoPlayNext_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["auto_play_next"] = (cb.Checked) ? "1" : "0";
        }

        private void update_checkHotSpot()
        {
            if (chkUseKeyBoard.Checked) //使用键盘，热点就一定要显示，但是可不跟随鼠标
            {
                chkShowHotspot.Checked = true;
                chkShowHotspot.Enabled = false;
                chkHotspotWithMouse.Enabled = true;
                chkHotspotWithMouse.Checked = (pubfun.str2int(ci["hotspot_with_mouse", "1"]) == 1);
            }
            else
            {
                chkShowHotspot.Enabled = true;
                chkShowHotspot.Checked = (pubfun.str2int(ci["show_hotspot", "1"]) == 1);
                chkHotspotWithMouse.Enabled = false;
                chkHotspotWithMouse.Checked = true;
            }

        }

        private void chkUseKeyBoard_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["use_keyboard"] = (cb.Checked) ? "1" : "0";
            update_checkHotSpot();
        }

        private void chkShowHotspot_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["show_hotspot"] = (cb.Checked) ? "1" : "0";
            update_checkHotSpot();
        }

        private void chkHotspotWithMouse_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["hotspot_with_mouse"] = (cb.Checked) ? "1" : "0";
        }

        private void chkShowStatisticsOnOver_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["game_finished_statistics"] = (cb.Checked) ? "1" : "0";
        }

        private void chkShowScoreListOnOver_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ci["game_finished_scorelist"] = (cb.Checked) ? "1" : "0";
        }


    }

}
